import request from "@/utils/request";
import type {
  GetRolePermissionListResponse,
  SavePermissionParams,
} from "./model/permissionModel";

enum Api {
  getRolePermissionListUrl = "/admin/acl/permission/toAssign",
  assignRolePermissionListUrl = "/admin/acl/permission/doAssign",
  getPermissionListUrl = "/admin/acl/permission",
  savePermissionUrl = "/admin/acl/permission/save",
}

/**
 * 获取角色的菜单列表
 * @param roleId 角色id
 * @returns GetRolePermissionListResponse
 */
export const getRolePermissionListApi = (roleId: string) => {
  return request.get<any, GetRolePermissionListResponse>(
    Api.getRolePermissionListUrl + `/${roleId}`
  );
};

/**
 * 分配角色菜单权限
 * @param roleId 角色id
 * @param permissionId 菜单id
 * @returns null
 */
export const assignRolePermissionListApi = (
  roleId: string,
  permissionId: string[]
) => {
  return request.post<any, null>(
    Api.assignRolePermissionListUrl,
    {},
    {
      params: {
        roleId,
        permissionId: permissionId.join(","),
      },
    }
  );
};

/**
 * 获取菜单列表
 * @returns GetRolePermissionListResponse
 */
export const getPermissionListApi = () => {
  return request.get<any, GetRolePermissionListResponse>(
    Api.getPermissionListUrl
  );
};

/**
 * 添加菜单
 * @param data SavePermissionParams
 * @returns null
 */
export const savePermissionApi = (data: SavePermissionParams) => {
  return request.post<any, null>(Api.savePermissionUrl, data);
};
